/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jfinance.UI;

import java.awt.Component;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import jfinance.Account;
import jfinance.Transaction;

/**
 *
 * @author efim
 */
public class AccountFrame extends javax.swing.JFrame {

    /**
     * Creates new form AccountFrame
     */
    public AccountFrame(Account account) {
        this.account = account;
        initComponents();
        List<Transaction> list =  account.getTransactions();
        for(Transaction t:list){
            transactionModel.addElement(t);
        }
    }
    
    private Account account;
    
    private DefaultListModel<Transaction> transactionModel = new DefaultListModel<>();
    
    public Account getAccount(){
        return account;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        transactionScrollPane = new javax.swing.JScrollPane();
        transactionList = new javax.swing.JList();
        nameTextField = new javax.swing.JTextField();
        transactionToolBar = new javax.swing.JToolBar();
        addTransactionButton = new javax.swing.JButton();
        editTransactionButton = new javax.swing.JButton();
        deleteTransactionButton = new javax.swing.JButton();
        amountLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        saveNameButton = new javax.swing.JButton();

        setTitle("Account info");

        transactionList.setBackground(new java.awt.Color(240, 240, 240));
        transactionList.setModel(transactionModel);
        transactionList.setCellRenderer(new ListCellRenderer(){
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Transaction v = (Transaction) value;
                TransactionBeanForm lPanel = new TransactionBeanForm(v);

                if (isSelected) {
                    lPanel.setBackground(list.getSelectionBackground());
                    lPanel.setForeground(list.getSelectionForeground());
                } else {
                    lPanel.setBackground(list.getBackground());
                    lPanel.setForeground(list.getForeground());
                }

                return lPanel;
            }
        });
        transactionScrollPane.setViewportView(transactionList);

        nameTextField.setBackground(new java.awt.Color(240, 240, 240));
        nameTextField.setFont(new java.awt.Font("Malgun Gothic", 0, 24)); // NOI18N
        nameTextField.setText(account.getName());
        nameTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                nameTextFieldPropertyChange(evt);
            }
        });

        transactionToolBar.setRollover(true);

        addTransactionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jfinance/UI/mini_icons/add.png"))); // NOI18N
        addTransactionButton.setToolTipText("Создать");
        addTransactionButton.setFocusable(false);
        addTransactionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addTransactionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        transactionToolBar.add(addTransactionButton);

        editTransactionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jfinance/UI/mini_icons/edit.png"))); // NOI18N
        editTransactionButton.setToolTipText("Редактировать");
        editTransactionButton.setFocusable(false);
        editTransactionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editTransactionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        transactionToolBar.add(editTransactionButton);

        deleteTransactionButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jfinance/UI/mini_icons/delete.png"))); // NOI18N
        deleteTransactionButton.setToolTipText("Удалить");
        deleteTransactionButton.setFocusable(false);
        deleteTransactionButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deleteTransactionButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        transactionToolBar.add(deleteTransactionButton);

        amountLabel.setFont(new java.awt.Font("Malgun Gothic", 0, 24)); // NOI18N
        amountLabel.setText(account.getAmount()+"");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        saveNameButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jfinance/UI/mini_icons/yes.png"))); // NOI18N
        saveNameButton.setToolTipText("Сохранить");
        saveNameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveNameButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(transactionToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(saveNameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, Short.MAX_VALUE)))
                    .addComponent(transactionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 117, Short.MAX_VALUE)
                        .addComponent(amountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(amountLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveNameButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(transactionToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(transactionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameTextFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_nameTextFieldPropertyChange
        // TODO add your handling code here:
       
    }//GEN-LAST:event_nameTextFieldPropertyChange

    private void saveNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveNameButtonActionPerformed
        // TODO add your handling code here:
        account.editName(nameTextField.getText());
    }//GEN-LAST:event_saveNameButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AccountFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AccountFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AccountFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AccountFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTransactionButton;
    private javax.swing.JLabel amountLabel;
    private javax.swing.JButton deleteTransactionButton;
    private javax.swing.JButton editTransactionButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton saveNameButton;
    private javax.swing.JList transactionList;
    private javax.swing.JScrollPane transactionScrollPane;
    private javax.swing.JToolBar transactionToolBar;
    // End of variables declaration//GEN-END:variables
}
